const { Util } = require("../../../utils/util");
const { API_SNZO_FILE_URL } = require("../../../utils/wxapi");
const wxapi = require("../../../utils/wxapi");

// pages/exchange/exhibition/exhibition.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: null,
    projectType: null,
    submenuId: null,
    activityIntro: null,
    sortOfData: null,
    listOfData: null,
    bodyParams: {
      artName: '',
      activityId: '',
      relatePeopleId: '',
      relatePeriodId: '',
      relateVenueId: '',
      standby5: '',
      showMajor: '',
      artStatus: 1,
    },
    contest: null,
    worksTypeId: null,
    pageParams: { pageNum: 1, pageSize: 10 },
    total: 0,
    isTriggered: false,
    worksImgUrl: API_SNZO_FILE_URL
  },

  /**
   * 获取活动内容
   */
  getActivityIntro() {
    wxapi.selectArtActiveById({ id: this.data.activityId }).then(res => {
      if (res.status === 200) {
        wx.setNavigationBarTitle({
          title: res.data.activityName,
        })
        this.setData({
          activityIntro: res.data
        })
        this.data.contest = res.data.contest;
        this.data.worksTypeId = res.data.worksTypeId;
        if (res.data.contest) {
          this.getOtherData();
        } else {
          this.getData();
        }
        this.getWorksType();
      }
    })
  },

  /**
   * 获取艺术品类别
   */
  getWorksType() {
    wxapi.getStNumWorksType({ standby4: this.data.worksTypeId }).then(res => {
      if (res.status === 200) {
        const sortOfData = res.data.map(item => { item.typeName = item.typeName.slice(0, 2); return item });
        this.setData({
          sortOfData
        })
      }
    })
  },

  /**
   * 获取数据
   */
  getData() {
    wx.showLoading({
      title: '加载中...',
    })
    this.data.bodyParams.standby5 = this.data.projectType;
    this.data.bodyParams.showMajor = this.data.submenuId;
    wxapi.getArtShowMsgByPayStatus(Util.trimNulls(this.data.bodyParams), this.data.pageParams).then(res => {
      this.setData({
        isTriggered: false
      })
      wx.hideLoading();
      if (res.status === 200) {
        if (this.data.pageParams.pageNum === 1) {
          this.data.listOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const listOfData = res.data.seachList;
        const newData = this.data.listOfData.concat(listOfData);
        this.setData({
          listOfData: newData,
          total: res.data.seachTotal,
          canScroll: true
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },

  /**
   * 获取特定活动展品
   */
  getOtherData() {
    wx.showLoading({
      title: '加载中...',
    })
    const bodyParams = {
      contest: this.data.contest,
      worksType: this.data.worksTypeId,
      standby6: this.data.submenuId,
      worksName: this.data.bodyParams.artName
    }
    const params = Util.trimNulls(this.data.pageParams);
    let pageParams = '';
    for (const key in params) {
      if (Object.hasOwnProperty.call(params, key)) {
        const value = params[key];
        pageParams += `&${key}=${value}`
      }
    }
    wxapi.selectByPass(Util.trimNulls(bodyParams), pageParams).then(res => {
      this.setData({
        isTriggered: false
      })
      wx.hideLoading();
      if (res.status === 200) {
        if (this.data.pageParams.pageNum === 1) {
          this.data.listOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const listOfData = res.data.data;
        const newData = this.data.listOfData.concat(listOfData);
        this.setData({
          listOfData: newData,
          total: res.data.total,
          canScroll: true
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },

  /**
   * 切换品区
   * @param {*} e 
   */
  projectChange(e) {
    const projectType = e.currentTarget.dataset.projectType;
    this.setData({
      projectType,
      submenuId: null
    })
    if (this.data.contest) {
      this.getOtherData();
    } else {
      this.getData();
    }
  },

  /**
   * 主题筛选
   * @param {*} e 
   */
  sortChange(e) {
    const submenuId = e.currentTarget.dataset.sortId;
    this.setData({
      submenuId: submenuId == 0 ? null : submenuId
    })
    if (this.data.contest) {
      this.getOtherData();
    } else {
      this.getData();
    }
  },

  /**
   * 预览艺术品
   * @param {*} e 
   */
  previewWorks(e) {
    const worksInfo = e.currentTarget.dataset.works;
    const thumbUrl = worksInfo.showPath ?? worksInfo.tabStnumWorkses[0].standby1;
    wx.previewMedia({
      sources: [
        {
          url: this.data.worksImgUrl + thumbUrl,
          type: 'image'
        }
      ],
    })
  },

  /**
   * 滑到最底部加载 获取分页数据
   */
  scrollToLower(e) {
    if (this.data.listOfData.length === this.data.total) {
      return;
    }
    this.setData({
      canScroll: false
    })
    this.data.pageParams.pageNum++
    if (this.data.contest) {
      this.getOtherData();
    } else {
      this.getData();
    }
  },

  /**
   * 下拉刷新
   */
  refresherToLoad() {
    this.data.pageParams.pageNum = 1;
    if (this.data.contest) {
      this.getOtherData();
    } else {
      this.getData();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.activityId) {
      this.setData({
        activityId: options.activityId
      })
      this.getActivityIntro();
    }
    if (options.projectType) {
      this.setData({
        projectType: options.projectType
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})